package com.dongdongshop.shiro;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.user.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class Realm extends AuthorizingRealm {

    @Reference
    private UserService userServiceI;
    /*执行授权逻辑
     * */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       /* System.out.println("执行授权逻辑");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //查询当前登录用户的perms权限
        TbSeller seller  = (TbSeller) SecurityUtils.getSubject().getPrincipal();
        List<String> perms = menuServiceI.getPermsByPid(person.getPid());
        authorizationInfo.addStringPermissions(perms);
        //*/
        return null;
    }

    /*执行认证逻辑
     * */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证逻辑");
        UsernamePasswordToken totken = (UsernamePasswordToken) authenticationToken;
        TbUser user = userServiceI.getUserByUserName(totken.getUsername());
        if (user != null) {
                return user == null ? null : new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), user.getUsername());
        }
        return null;
       /* }

        return null;*/

    }
}
